<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * base_url/article
 *
 * @package    controllers
 * @author     Bertrand Kintanar <bertrand.kintanar@gmail.com>
 * @version    $Id: article.php 214 2011-12-08 21:01:26Z bertrand.kintanar@gmail.com $
 * @copyright  &copy; 2011 NaCl.org WebGroup
 *
 */
class Article extends MY_Controller {

    
    public function __construct()
    {
        parent::__construct();
        parent::_get_script(__FILE__);
        
        $this->load->library('pagination');
        
        $this->_article_obj = new \models\Article;
        $_articles    = $this->_article_obj->getArticles();
        
        if (ENVIRONMENT != 'development')
        {
            $this->load->spark('twitterfetcher/1.0.4');
        }
    }
    
    public function index()
    {
        
        $config['base_url']   = $this->config->item('base_url').'article/index/';
        $config['total_rows'] = count($_articles);
        $config['per_page']   = 5;
        $config['num_links']  = 5;
        
        $this->pagination->initialize($config);

        $this->qb->add('select', 'a')
            ->add('from', 'models\Article a')
               ->setMaxResults( $config['per_page'] );
        
        if ( $this->uri->segment(3) )
        {
            $this->qb->setFirstResult( $this->uri->segment(3) );
        }
        
        $query = $this->qb->getQuery();
        
        $articles = $query->getResult();

        $this->smarty->assign("paginateLinks", $this->pagination->create_links());
        $this->smarty->assign("articles", $articles);
        
        $this->smarty->assign('main_content', 'articles.tpl');
        $this->smarty->view();
    }

    public function view()
    {
        $this->load->library('pagination');
        
        $_article_obj = new \models\Article;
        
        $archives = $_article_obj->getArticleArchive();
        
        if ( $this->uri->segment(3) )
        {
            $_article_id = $this->uri->segment(3);
            $_article = $_article_obj->getArticleById($_article_id);
        }
        if ($_article)
        {
            $this->smarty->assign("article", $_article);
            $_tags = $_article->getArticleTags();
            
            $_article_next = $_article_obj->getNextArticleById($_article_id);
            $_article_prev = $_article_obj->getPrevArticleById($_article_id);
            
            if ($_article_next)
            {
                $this->smarty->assign("article_next", $_article_next);
            }
            
            if($_article_prev)
            {
                $this->smarty->assign("article_prev", $_article_prev);
            }
            
            if ($_tags)
            {
                $this->smarty->assign('tags', $_tags);
            }
        }
        
        // Start - get twitter feeds for @NaClOrg
        $twitterId = 'NaClOrg';
        $this->smarty->assign('twitterId', $twitterId);
        
        if (ENVIRONMENT != 'development')
        {
            $tweets = $this->twitterfetcher->getTweets(array('twitterID' => $twitterId, 'usecache' => false, 'count' => 5, 'numdays' => 30));
            $this->smarty->assign('tweets', $tweets);
        }
        // End - get twitter feeds for @NaClOrg

        // TODO: Move these variables to a config file
        $this->smarty->assign('page_title', $_article->article_title);
        
        // Load the template from the views directory
        $this->smarty->assign('archives', $archives);
        $this->smarty->assign('main_content', 'article_view.tpl');
        $this->smarty->view();
    }
    
    public function write()
    {
        $this->smarty->assign('load_forms', true);
        $this->smarty->assign('load_tiny_mce', true);
        $this->smarty->assign('main_content', 'article_write.tpl');
        $this->smarty->view();
    }
    
    public function get_tags()
    {
        $_tag_obj = new \models\Tag;
        $_tags    = $_tag_obj->getTags();
        
        $json = array();
        foreach ($_tags as $_tag)
        {
            $tag['value'] = $_tag->tag_id;
            $tag['name']  = $_tag->tag_name;
            
            $json[]       = $tag;
        }
        
        print( json_encode( $json ) );
    }
}
